package com.tangqi.android;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FundJiaActivity extends Activity {
	
	private static final String TAG = "SipFundCalculator";
	
	//甲类公积金总比例	
	public static final double TOTAL_FUND_RATIO = 0.47;
	
	public static final double FUND1_RATIO = 0.02;
	public static final double FUND2_RATIO = 0.06;
	public static final double FUND3_RATIO = 0.03;
	public static final double FUND4_RATIO = 0.16;
	public static final double FUND5_RATIO = 0.14;
	
	public static final double FUND6_RATIO = 0.01;
	public static final double FUND7_RATIO = 0.02;
	public static final double FUND8_RATIO = 0.01;
	public static final double FUND9_RATIO = 0.01;
	public static final double FUND10_RATIO = 0.01;
	
	//Button[计算]
	private Button calculatorBtn;
	//Button[清空]
	private Button resetBtn;
    //税前收入
	private EditText grossIncomeTxt;
	//个人公积金
	private EditText personalFundTxt;
	private EditText deductFundIncomeTxt;
	//应缴个税
	private EditText incomeTaxTxt;
	private EditText deductTaxIncomeTxt;
	//公积金总额
	private EditText fundTxt;

	private TextView fundTV1;
//	private EditText fundTxt1;
	private TextView fundTV2;
	private TextView fundTV3;
	private TextView fundTV4;
	private TextView fundTV5;
	private TextView fundTV6;
	private TextView fundTV7;
	private TextView fundTV8;
	private TextView fundTV9;
	private TextView fundTV10;	
	
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jia);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		calculatorBtn = (Button) findViewById(R.id.calculatorBtn);
		calculatorBtn.setOnClickListener(listener);

		resetBtn = (Button) findViewById(R.id.resetBtn);
		resetBtn.setOnClickListener(listener);

		grossIncomeTxt = (EditText) findViewById(R.id.grossIncomeTxt);
		fundTxt = (EditText) findViewById(R.id.fundTxt);
		
		personalFundTxt = (EditText) findViewById(R.id.personalFundTxt);
		
		deductFundIncomeTxt = (EditText) findViewById(R.id.deductFundIncomeTxt);
		incomeTaxTxt = (EditText) findViewById(R.id.incomeTaxTxt);
		deductTaxIncomeTxt = (EditText) findViewById(R.id.deductTaxIncomeTxt);
		
		fundTV1 = (TextView) findViewById(R.id.fundTV1);
//		fundTxt1 = (EditText) findViewById(R.id.fundTxt1);
		fundTV2 = (TextView) findViewById(R.id.fundTV2);
		fundTV3 = (TextView) findViewById(R.id.fundTV3);
		fundTV4 = (TextView) findViewById(R.id.fundTV4);
		fundTV5 = (TextView) findViewById(R.id.fundTV5);
		
		fundTV6 = (TextView) findViewById(R.id.fundTV6);
		fundTV7 = (TextView) findViewById(R.id.fundTV7);
		fundTV8 = (TextView) findViewById(R.id.fundTV8);
		fundTV9 = (TextView) findViewById(R.id.fundTV9);
		fundTV10 = (TextView) findViewById(R.id.fundTV10);
		
		
	}

	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Button btn = (Button) v;
			Log.i(TAG, btn.getId() + " is clicked");
			switch (btn.getId()) {
			case R.id.calculatorBtn:
				calculateAll();
				break;
			case R.id.resetBtn:
				resetAll();
				break;
			}
		}
	};

	public void calculateAll() {
		//缴费基数
		double grossIncome = Double.valueOf(grossIncomeTxt.getText().toString());
		
		//判断缴费基数是否超出范围
		String warningMsg = CalUtil.checkBound("JIA", grossIncome);
		if(warningMsg!=null){
			new AlertDialog.Builder(this).setMessage(warningMsg).show();
			return;
		}
		
		//公积金总额
		double fund = 0.47 * grossIncome;
		
		//个人缴纳公积金
		double persnalFund = 0.19 * grossIncome;
		
		//扣除个人所交公积金后所得
		double deductFundIncome = 0.81 * grossIncome;
		
		//个人所得税
		double incomeTax = CalUtil.calculateIIT(deductFundIncome);
		
		//实际所得
		double deductTaxIncome = deductFundIncome - incomeTax;

		Log.i(TAG, "grossIncome=" + grossIncome);
		Log.i(TAG, "fund=" + fund);
		Log.i(TAG, "persnalFund=" + persnalFund);
		Log.i(TAG, "deductFundIncome=" + deductFundIncome);
		Log.i(TAG, "incomeTax=" + incomeTax);
		Log.i(TAG, "deductTaxIncome=" + deductTaxIncome);
		
		fundTxt.setText(String.valueOf(CalUtil.tranFormat(fund)));
		personalFundTxt.setText(String.valueOf(CalUtil.tranFormat(persnalFund)));
		deductFundIncomeTxt.setText(String.valueOf(CalUtil.tranFormat(deductFundIncome)));
		incomeTaxTxt.setText(String.valueOf(CalUtil.tranFormat(incomeTax)));
		deductTaxIncomeTxt.setText(String.valueOf(CalUtil.tranFormat(deductTaxIncome)));
		
//		fundTxt1.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND1_RATIO)));
		fundTV1.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND1_RATIO)));
		fundTV2.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND2_RATIO)));
		fundTV3.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND3_RATIO)));
		fundTV4.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND4_RATIO)));
		fundTV5.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND5_RATIO)));
		
		fundTV6.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND6_RATIO)));
		fundTV7.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND7_RATIO)));
		fundTV8.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND8_RATIO)));
		fundTV9.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND9_RATIO)));
		fundTV10.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND10_RATIO)));
		
	}

	public void resetAll() {
		grossIncomeTxt.setText(null);
		fundTxt.setText(null);
		deductFundIncomeTxt.setText(null);
		incomeTaxTxt.setText(null);
		deductTaxIncomeTxt.setText(null);
		
		fundTV1.setText(null);
//		fundTxt1.setText(null);
		fundTV2.setText(null);
		fundTV3.setText(null);
		fundTV4.setText(null);
		fundTV5.setText(null);
		fundTV6.setText(null);
		fundTV7.setText(null);
		fundTV8.setText(null);
		fundTV9.setText(null);
		fundTV10.setText(null);

	}
	
/*	public double tranFormat(double bf){
		return new Double(new DecimalFormat(".00").format(bf));
	}*/
}
